package homework;

import java.util.Stack;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LENOVO
 * Date: 2024-08-08
 * Time: 16:17
 */
public class Test {
    public static int evalRPN(String[] tokens) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < tokens.length; i++) {
            if(tokens[i].equals("+") || tokens[i].equals("-")||tokens[i].equals("*")||tokens[i].equals("/")){
                int num2 = stack.pop();
                int num1 = stack.pop();
                int sum = 0;
                switch (tokens[i]){
                    case "+":
                        sum = num1 + num2;
                        stack.push(sum);
                        break;
                    case "-":
                        sum = num1 - num2;
                        stack.push(sum);

                        break;
                    case "*":
                        sum = num1 * num2;
                        stack.push(sum);

                        break;
                    case "/":
                        sum = num1 / num2;
                        stack.push(sum);
                        break;
                }
            }else{
                stack.push(Integer.parseInt(tokens[i]));
            }
        }
        int ret = stack.pop();
        return ret;
    }


    public boolean isValid(String s) {//括号匹配问题
        //[()]    [[]   ][[   (]
        Stack<Character> stack = new Stack<>();
        for (int i = 0; i < s.length(); i++) {
            char ch = s.charAt(i);
            if(ch =='(' ||ch == '[' ||ch == '{'){//注意我们现在是字符比较，字符用单引号！
                stack.push(ch);
            }else{
                if(stack.empty()){
                    return false;
                }
                //匹配
                if((stack.peek() == '(' && ch == ')')||(stack.peek() == '[' && ch == ']')||(stack.peek() == '{' && ch == '}')){
                    stack.pop();
                }else{
                    return false;
                }
            }
        }
        if(!stack.empty()){
            return false;
        }
        return true;
    }







    public static void main(String[] args) {
        String[] tokens = new String[]{"4","13","5","/","+"};
        int ret = evalRPN(tokens);
        System.out.println(ret);
    }
}
